/**
 * Created by kamlyli on 2017/7/22.
 */
export default class NetUtil {
  // 发送Post请求，返回json
  static postJson (header, url, data) {
    return NetUtil.requestJson(header, url, data, 'post')
  }

  // 发送Get请求，返回json
  static getJson (header, url, data) {
    return NetUtil.requestJson(header, url, data, 'get')
  }

  // 发送Get请求，返回json
  static delJson (header, url, data) {
    return NetUtil.requestJson(header, url, data, 'DELETE')
  }

  // 发送请求
  static requestJson (header, url, data, method) {
    header = header || {}
    data = data || {}
    return new Promise(function (resolve, reject) {
      wx.request({
        'method': method,
        'url': url,
        'data': data,
        'header': {
          'Content-Type': 'application/json',
          '_aq': header
        },
        success: function (res) {
          console.log(res)
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  }

  // 上传文件
  static uploadFile (url, filePath, name, data) {
    data = data || {}
    return new Promise(function (resolve, reject) {
      wx.uploadFile({
        'url': url,
        'filePath': filePath,
        'name': name,
        'formData': data,
        success: function (res) {
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  }
}
